source sim_coverage
puts $env(SIM_COVERAGE)
# Set local tcl variables
set sim_module tb
set sim_name "" 
set sim_instances {$sim_module $sim_name glbl}

# variables less likely to change
set wavefilename $sim_module.wlf
set time_resolution 1ps

# Arguments passed into tcl file that need to be modified and passed to vsim as +plusargs.
# First extract only the arguments passed in using "+"
#regexp -all {\s*\+.*\S} $argv inputargs
# Second substitute +pli with -pli
#regsub -all {\s*\+pli} $inputargs " -pli " pli
# Third substitute +L with -L
#regsub -all {\s*\+L} $pli " -L " libs 
# Fourth substitute the + deliminator to be a space and a + so that $test$plusargs
# works correctly in the testbench.
#regsub -all {\+} $libs " +" plusargs

# Call to compile verilog files
#eval vsim -quiet -novopt -t $time_resolution $sim_instances $plusargs
# Read in tcl file
do compile.tcl
# compile libraries
dcom
# compile RTL 
com_rtl
# load simulation
if {$env(SIM_COVERAGE) == "cov_en"} {
    eval vsim -coverage -quiet -t $time_resolution -L altera_ver -L lpm_ver -L sgate_ver -L altera_mf_ver -L altera_lnsim_ver -L fiftyfivenm_ver -L rtl_work -L work +nowarnTSCALE -logfile tb.txt +alltests tb 
} else {
    eval vsim -quiet -t $time_resolution -L altera_ver -L lpm_ver -L sgate_ver -L altera_mf_ver -L altera_lnsim_ver -L fiftyfivenm_ver -L rtl_work -L work +nowarnTSCALE -logfile tb.txt +alltests tb 
}


set IgnoreWarning 1
set BreakOnAssertion 3
onbreak {resume}

# call wave file, branch based on batch_mode
if [batch_mode] {
    # do nothing in batch mode
} elseif { [file exists wave.do] == 1} {
    do wave.do
} else {
    log /$sim_module/DUT/*
}

# run the simulation -all
run -all
if {$env(SIM_COVERAGE) == "cov_en"} {
    coverage report -file code_coverage.txt -byfile -assert -directive -cvg -codeAll
    coverage save $sim_module.ucdb
    vcover report -html $sim_module.ucdb
}

# Exit in batch mode, not in GUI mode
if [batch_mode] {
    quit -f
} else {
    # save wlf file for analysis
    #dataset save sim wlf/$wavefilename
}

# tcl commands for reference
# 
# interactive mode: tcl shortcuts
# do compile.tcl (re-load compile.tcl)
# dcom           (re-compile libraries)
# com_rtl        (re-compile rtl)
# r_rtl          (re-run simulation)

